//
//  RegisterModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/9.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

enum YSGLoginSubViewType {
    case register
    case forget
    
    var description: String {
        switch self {
        case .register: return "注 册"
        case .forget: return "确 认"
        }
    }
}

class RegisterModel: NSObject {
    var titles: [String] = []
    
    var name: String = ""
    var idCard: String = ""
    var provinceId: String = ""
    var cityId: String = ""
    var regionId: String = ""
    var address: String = ""
    var phone: String = ""
    var code: String = ""
    var password: String = ""
    var area: String = ""
    var repassword: String = ""
    
    /// 审核阶段
    var isCheck: Bool = false
    var version: String = ""
    
    var numberOfRows: Int { return titles.count }
    
    var isEnable: Bool {
        return !provinceId.isEmpty &&
            !cityId.isEmpty &&
            !regionId.isEmpty &&
            !name.isEmpty &&
            !idCard.isEmpty &&
            !address.isEmpty &&
            !phone.isEmpty &&
            !code.isEmpty &&
            !password.isEmpty
    }
    
    var codeBtnEnable: Bool {
        return !phone.isEmpty && phone.isPhone()
    }
}

extension RegisterModel {
    /// 检测是否注册过
    func checkRegistered(success: @escaping () -> Void,
                         failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = ["idcard": idCard, "phone": phone.encryptWithAES()]
        HttpTool.shared.request(target: ApiManager.checkRegistered(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                success()
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
    
    /// 获取短信验证码
    func sendSmsCode(success: @escaping () -> Void, failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = ["phone": phone.encryptWithAES()]
        HttpTool.shared.request(target: ApiManager.sendSmsCode(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                success()
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
    
    /// 注册
    func register(success: @escaping () -> Void, failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = [
            "name": name,
            "idcard": idCard,
            "phone": phone.encryptWithAES(),
            "code": code,
            "password": password.encryptWithAES(),
            "province": provinceId,
            "city": cityId,
            "region": regionId,
            "addr": address,
            "vision": version
        ]
        HttpTool.shared.request(target: ApiManager.register(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                success()
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
    
    /// 忘记密码
    static func forgetPassword(phone: String,
                               code: String,
                               password: String,
                               finished: @escaping (_ errorMsg: String?, _ code: HttpCode) -> Void)
    {
        let paras = [
            "phone": phone,
            "code": code,
            "password": password
        ]
        HttpTool.shared.request(target: ApiManager.forgetPassword(paras: paras), success: { (json, code) in
            finished(json["message"].description, code)
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    
    /// 获取审核版本号
    static func getAppVersion(finished: @escaping (String) -> Void) {
        HttpTool.shared.request(target: ApiManager.getAppVersion, success: { (json, code) in
            finished(json["data"].description)
        }) { _ in
            finished("")
        }
    }
}
